import { login, logout, getInfo, submit, putavatar, changepassword } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import ro from 'element-ui/src/locale/lang/ro'

const getDefaultState = () => {
  return {
    token: getToken(),
    uid: '',
    name: '',
    avatar: '',
    roles: [],
    role: '',
    password: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ROLE: (state, roles) => {
    state.role = roles
  },
  SET_PASSWORD: (state, password) => {
    state.password = password
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { uid, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ uid: uid.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        commit('SET_UID', uid)
        commit('SET_PASSWORD', password)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        const { uid, name, avatar, password, roles } = data

        if (!roles || roles.length <= 0) {
          reject('getInfo: roles must be a non-null array!')
        }

        commit('SET_UID', uid)
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_PASSWORD', password)
        commit('SET_ROLES', roles)
        commit('SET_ROLE', roles[0])

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  submit({ commit }, uid_newname) {
    const { uid, name } = uid_newname
    return new Promise((resolve, reject) => {
      submit({ uid: uid, name: name }).then(response => {
        const { data } = response

        if (!data) {
          return reject('username get error.')
        }

        commit('SET_NAME', data.name)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  putavatar({ commit }, uid_newavatar) {
    const { uid, avatar } = uid_newavatar
    return new Promise((resolve, reject) => {
      putavatar({ uid: uid, avatar: avatar }).then(response => {
        const { data } = response

        if (!data) {
          return reject('avatar get error.')
        }

        commit('SET_AVATAR', data.avatar)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  changepassword({ commit }, uid_newPassword) {
    const { uid, password } = uid_newPassword
    return new Promise((resolve, reject) => {
      changepassword({ uid: uid, password: password }).then(response => {
        const { data } = response

        if (!data) {
          return reject('password get error.')
        }

        commit('SET_PASSWORD', data.password)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

